<?php

namespace App\Models;

use Dcat\Admin\Traits\HasDateTimeFormatter;

use Illuminate\Database\Eloquent\Model;

class Customer extends Model
{
	use HasDateTimeFormatter;
    protected $table = 'customer';

    public function scopeSearch($query, $params)
    {
        if(!empty($params['customer_name'])){
            $query->where('customer_name', 'like','%'.$params['customer_name'].'%');
        }
        if(!empty($params['customer_phone'])){
            $query->where('customer_phone', 'like','%'.$params['customer_phone'].'%');
        }
        if(!empty($params['another_name'])){
            $query->where('another_name', 'like','%'.$params['another_name'].'%');
        }
        if(!empty($params['province'])){
            $query->where('province',$params['province']);
        }
        if(!empty($params['city'])){
            $query->where('city',$params['city']);
        }
        if(!empty($params['county'])){
            $query->where('county',$params['county']);
        }
        if(!empty($params['town'])){
            $query->where('town',$params['town']);
        }
        if(!empty($params['village'])){
            $query->where('village',$params['village']);
        }
        if(!empty($params['team'])){
            if($params['team'] != '请选择生产队'){
                $query->where('team',$params['team']);
            }
        }
        return $query;
    }

    public function county(){
        return $this->hasOne(Address::class,'id','county');
    }

    public function town(){
            return $this->hasOne(Address::class,'id','town');
    }

    public function village(){
        return $this->hasOne(Address::class,'id','village');
    }

}
